package com.vr.cloudpicture.manager.websocket.disruptor;

import com.lmax.disruptor.WorkHandler;
import com.vr.cloudpicture.manager.websocket.model.PictureEditEvent;
import com.vr.cloudpicture.manager.websocket.model.PictureEditRequestMessage;
import com.vr.cloudpicture.manager.websocket.strategy.PictureEditMessageStrategy;
import com.vr.cloudpicture.manager.websocket.strategy.PictureEditMessageStrategyFactory;
import com.vr.cloudpicture.model.dto.user.UserLogin;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

/**
 * 图片编辑事件的消费者
 *
 * @author hzh
 * @date 2025/05/28
 */
@Component
@AllArgsConstructor
public class PictureEditEventWorkHandler implements WorkHandler<PictureEditEvent> {
    private final PictureEditMessageStrategyFactory pictureEditMessageStrategyFactory;
    @Override
    public void onEvent(PictureEditEvent pictureEditEvent) throws Exception {
        // 拿到消息参数
        Long pictureId = pictureEditEvent.getPictureId();
        UserLogin user = pictureEditEvent.getUser();
        WebSocketSession session = pictureEditEvent.getSession();
        PictureEditRequestMessage requestMessage = pictureEditEvent.getPictureEditRequestMessage();
        String type = requestMessage.getType();

        // 根据消息类型用不同的策略处理
        PictureEditMessageStrategy strategy = pictureEditMessageStrategyFactory.getStrategyByMessageType(type);
        strategy.handle(requestMessage, session, user, pictureId);
    }
}
